layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'chakuan_list',
		where_json = {
			_o: 'pk_id',
			_b: 'desc',
			view_state: 'no'
		},
		form_filter = 'chakuan_search_form';
	// 渲染表格
	if (load_data === true) {
		table.render({
			elem: '#' + table_filter,
			id: table_filter,
			url: pikuan_list_url,
			method: 'post',
			height: 'full-200',
			where: where_json,
			cols: [pikuan_cols],
			page: {
				layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
				limit: 30,
				theme: '#393d49',
				groups: 8
			},
			autoSort: false,
			toolbar: '#chakuanToolbar',
			initSort: {
				field: 'pk_id',
				type: 'desc'
			}
		});
	}
	// 编辑表格
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var returned_id = data.returned_id;
		console.log(data, e)
		switch (e) {
			case 'zhiyi':
				$('#zhiyi_order').val(data.order_nos.replace(/,/g, '\n'))
				layer.open({
					type: 1,
					area: ['800px', '500px'],
					title: '修改质疑单号', //不显示标题
					content: $('.zhiyi_info'),
					shade: 0,
					btn: ['确定提交', '取消'],
					yes: function(index, layero) {
						let post_json = {
							pk_id: data.pk_id,
							zhiyi_order: $('#zhiyi_order').val().replace(/\n/g, ',')
						}
						console.log(post_json)
					}
				});
				break;
			default:
				break;
		}
	})

	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		var filter_url = $(this).attr('data-url');
		console.log(filter_url)
		switch (obj.event) {
			case 'importPikuan':
				layer.open({
					type: 2,
					anim: -1,
					title: '匹款',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: true,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: filter_url
				})
				break;
		}
	})


	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})

	// 回款时间
	var pay_time_start = laydate.render({
		elem: 'input[name="pay_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				pay_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['pay_time_start'] = value;
		}
	});
	var pay_time_end = laydate.render({
		elem: 'input[name="pay_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				pay_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['pay_time_end'] = value;
		}
	});
	// 认款时间
	var create_time_start = laydate.render({
		elem: 'input[name="create_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['create_time_start'] = value;
		}
	});
	var create_time_end = laydate.render({
		elem: 'input[name="create_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			where_json['create_time_end'] = value;
		}
	});

	form.render();

	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		where_json[name] = value;
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});

	form.on('select', function(data) {
		where_json[data.elem.attributes['lay-filter'].value] = data.value;
	})



	form.on('submit(chakuan_search_form)', function(data) {
		layer.load();
		where_json.view_state = 'yes'
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
		return false
	})

})